package user_daos

import (
	"errors"
	"log"
	"rabbit_love/src/model"
	"rabbit_love/src/service/client/utils"
	"time"
)

type UserInfoStruct struct {
	NickName        string    `json:"nickName"`
	UserName        string    `json:"userName"`
	IconUrl         string    `json:"iconUrl"`
	Sex             string    `json:"sex"`
	LoverRelationId int       `json:"loverRelationId"`
	BirthDay        time.Time `json:"birthDay"`
}

/*
userInfoGet 获取用户基本信息
*/
func userInfoGet(userId int) (map[string]interface{}, error) {
	var (
		userInfos UserInfoStruct
		clientDB  = utils.ClientDB
		err       error
	)

	err = clientDB.Model(&model.UserTable{}).
		Select("userName UserName, nickName NickName, iconUrl IconUrl, sex Sex, loverRelationId LoverRelationId, birthDay BirthDay").
		Where("id = ?", userId).
		Scan(&userInfos).Error
	if err != nil {
		return nil, errors.New("没有找到对应的用户")
	}

	var indexMap = make(map[string]interface{})

	if userInfos.LoverRelationId != 0 {
		indexMap, err = indexMapGet(userInfos.LoverRelationId, userId)

	} else {
		var loverInvitePassiveCount int64
		loverInvitePassiveCount, _ = LoverInvitePassiveCountGet(userId)
		indexMap["loverInvitePassiveCount"] = loverInvitePassiveCount

		var loverInviteInitiativeCount int64
		loverInviteInitiativeCount, _ = LoverInviteInitiativeCountGet(userId)
		indexMap["loverInviteInitiativeCount"] = loverInviteInitiativeCount
	}

	indexMap["userInfos"] = userInfos

	return indexMap, nil
}

type loverIndexMap struct {
	LoverSpaceName string    `json:"loverSpaceName"`
	LoverName      string    `json:"loverName"`
	StartTime      time.Time `json:"startTime"`
	UserName       string    `json:"userName"`
	IconUrl        string    `json:"iconUrl"`
	BirthDay       string    `json:"birthDay"`
}

/*
indexMapGet 获取日常信息的数量
*/
func indexMapGet(loverId, userId int) (map[string]interface{}, error) {
	var (
		loverIndex loverIndexMap
		clientDB   = utils.ClientDB
		err        error
	)

	err = clientDB.Model(&model.LoverRelationTable{}).
		Select("lover_relation_table.loverName LoverSpaceName, lover_relation_table.startTime StartTime, ut.birthDay BirthDay, ut.userName UserName, lnt.targetNickName LoverName, ut.iconUrl IconUrl").
		Where("lover_relation_table.id = ?", loverId).
		Joins("LEFT JOIN user_table ut on ut.loverRelationId = ? and ut.id != ?", loverId, userId).
		Joins("LEFT JOIN lover_nick_name_table lnt on lnt.loverRelationId = ? and lnt.userId != ?", loverId, userId).
		Scan(&loverIndex).Error
	if err != nil {
		log.Println("indexCountGet => LoverRelationTable Err: ", err.Error())
		return nil, errors.New("获取信息失败")
	}

	var (
		signCount  int64
		dailyCount int64
	)
	err = clientDB.Model(&model.DailyTable{}).Where("`loverRelationId` = ? and `type` = 'sign' and `userId` != ? and `read` = FALSE", loverId, userId).Count(&signCount).Error
	if err != nil {
		log.Println("indexCountGet => signCount Err: ", err.Error())
		return nil, errors.New("获取信息失败")
	}

	err = clientDB.Model(&model.DailyTable{}).Where("`loverRelationId` = ? and `type` = 'daily' and `userId` != ? and `read` = FALSE", loverId, userId).Count(&dailyCount).Error
	if err != nil {
		log.Println("indexCountGet => dailyCount Err: ", err.Error())
		return nil, errors.New("获取信息失败")
	}

	var indexMap = make(map[string]interface{})

	indexMap["loverRelation"] = loverIndex
	indexMap["dataMap"] = map[string]interface{}{
		"dailyCount": dailyCount,
		"signCount":  signCount,
	}
	return indexMap, nil
}

/*
LoverInvitePassiveCountGet 获取被邀请成为情侣的数量， 未查看
*/
func LoverInvitePassiveCountGet(userId int) (int64, error) {
	var (
		loverInviteCount int64
		clientDB         = utils.ClientDB
		err              error
	)

	if err = clientDB.Model(&model.LoverInviteTable{}).Where("`to` = ? and `read` = FALSE", userId).Count(&loverInviteCount).Error; err != nil {
		log.Println("LoverInviteCountGet Err: ", err.Error())
		return 0, errors.New("查询失败")
	}
	return loverInviteCount, nil
}

/*
LoverInviteInitiativeCountGet 获取主动邀请恋人被查看的数量， 已被查看
*/
func LoverInviteInitiativeCountGet(userId int) (int64, error) {
	var (
		loverInviteCount int64
		clientDB         = utils.ClientDB
		err              error
	)

	if err = clientDB.Model(&model.LoverInviteTable{}).Where("`from` = ? and `read` = TRUE", userId).Count(&loverInviteCount).Error; err != nil {
		log.Println("LoverInviteCountGet Err: ", err.Error())
		return 0, errors.New("查询失败")
	}
	return loverInviteCount, nil
}

func GetLoverUserId(userId, loverId int) int64 {
	var (
		LoverInfo model.UserTable
		clientDB  = utils.ClientDB
		err       error
	)

	if err = clientDB.Model(&model.UserTable{}).Select("id").Where("`loverRelationId` = ? and `id` != ?", loverId, userId).First(&LoverInfo).Error; err != nil {
		return 0
	}
	return LoverInfo.ID
}
